﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;

namespace testApp
{
    public class DataManager
    {
        static DataManager _active = null;
        public static DataManager Active
        {
            get
            {
                if (_active == null)
                    _active = new DataManager();
                return _active;
            }
        }

        private DataManager()
        {
        }

        DataClassesDbCompanyDataContext _db = null;
        public DataClassesDbCompanyDataContext DB
        {
            get
            {
                if (_db == null)
                    _db = new DataClassesDbCompanyDataContext();
                return _db;
            }
        }


        #region WorkWithDeps
        public IEnumerable<Department> GetParentDepartments()
        {
            //  db.Refresh(RefreshMode.KeepChanges,
            var deps = from dep in db.Departments
                       where dep.ParentDepartmentID == 0
                       select dep;
            return deps;
        }

        public Department GetDepartmentByID(int depID)
        {
            var deps = (from dep in db.Departments
                        where dep.DepartmentID == depID
                        select dep).SingleOrDefault();
            return deps;
        }


        public IEnumerable<Department> GetDepartmentsAll()
        {
            var deps = from dep in db.Departments
                       select dep;
            return deps;
        }

        public IEnumerable<Department> GetDepartmentsAllExceptID(int exceptID)
        {
            var deps = from dep in db.Departments
                       where dep.DepartmentID != exceptID
                       select dep;
            return deps;
        }

        public IEnumerable<Department> GetDepartmentsByParentID(int depParentID)
        {
            var deps = from dep in db.Departments
                       where dep.ParentDepartmentID == depParentID
                       select dep;
            return deps;
        }

        public int AddNewDepartment(Department obj)
        {
            int result;
            result = 0;
            if ((obj.ParentDepartmentID < 0) || (obj.ParentDepartmentID == null))
            {
                obj.ParentDepartmentID = 0;
            }
            if (obj.CreateDate == null)
            {
                obj.CreateDate = DateTime.Now;
            }
            db.Departments.InsertOnSubmit(obj);
            try
            {

                db.SubmitChanges();
                result = obj.DepartmentID;
            }
            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show(e.Message);
                result = -1;
            }
            return result;
        }

        public int DelDepartmentByID(int depID)
        {
            var empInDep = from emps in db.Employees
                           where emps.DepartmentID == depID
                           select emps;
            if (empInDep != null)
            {
                db.Employees.DeleteAllOnSubmit(empInDep);
            }
            db.SubmitChanges();
            var dep = from deps in db.Departments
                      where deps.ParentDepartmentID == depID
                      select deps;
            if (dep != null)
            {
                db.Departments.DeleteAllOnSubmit(dep);
            }
            db.SubmitChanges();
            var sdep = from deps in db.Departments
                       where deps.DepartmentID == depID
                       select deps;
            if (sdep != null)
            {
                db.Departments.DeleteAllOnSubmit(sdep);
            }
            db.SubmitChanges();
            return 1;
        }

        public int EditDepartmentByID(Department obj)
        {
            int result;
            result = 0;
            var dep = from deps in db.Departments
                      where deps.DepartmentID == obj.DepartmentID
                      select deps;
            foreach (Department d in dep)
            {
                d.DepartmentID = obj.DepartmentID;
                d.DepartmentName = obj.DepartmentName;
                d.CreateDate = obj.CreateDate;
                d.ParentDepartmentID = obj.ParentDepartmentID;
            }
            try
            {
                db.SubmitChanges();
                result = obj.DepartmentID;
            }
            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show(e.Message);
                result = -1;
            }
            return result;
        }

        #endregion

        #region WorkWithEmployee

        public IEnumerable<vw_EmployeeList> GetEmployeeListByDepID(int depID)
        {
            var query = from emps in db.vw_EmployeeLists
                        where emps.DepartmentID == depID
                        select emps;
            return query;
        }

        public IEnumerable<vw_EmployeeList> GetEmployeeListByParentDepID(int parentDepID)
        {
            var query = from emps in db.vw_EmployeeLists
                        where emps.ParentDepartmentID == parentDepID
                        select emps;
            return query;
        }

        public Employee GetEmployeeByID(int empID)
        {
            var query = (from emps in db.Employees
                         where emps.EmployeeID == empID
                         select emps).SingleOrDefault();
            return query;
        }
        public List<vw_EmployeeList> GetEmployeeListByIDList(List<int> ids)
        {
            List<vw_EmployeeList> lstEmp = new List<vw_EmployeeList>();
            foreach (int id in ids)
            {
                var query = (from emps in db.vw_EmployeeLists
                             where emps.DepartmentID == id
                             select emps).ToList();
                foreach (var emp in query)
                {
                    lstEmp.Add(emp);
                }
            }
            return lstEmp;
        }

        public int EditEmployee(Employee obj)
        {
            int result;
            result = 0;
            var query = from emp in db.Employees
                        where emp.EmployeeID == obj.EmployeeID
                        select emp;
            foreach (Employee em in query)
            {
                //   em.EmployeeID = obj.EmployeeID;
                em.Name = obj.Name;
                em.Surname = obj.Surname;
                em.SecondName = obj.SecondName;
                em.Birthdate = obj.Birthdate;
                em.WorkDate = obj.WorkDate;
                em.SexID = obj.SexID;
                em.PostID = obj.PostID;
                em.Money = obj.Money;
                em.Phone = obj.Phone;
                em.email = obj.email;
                em.Info = obj.Info;
                em.DepartmentID = obj.DepartmentID;

            }
            try
            {
                db.SubmitChanges();
                result = obj.EmployeeID;
            }
            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show(e.Message);
                result = -1;
            }
            return result;
        }

        public int AddNewEmployee(Employee obj)
        {
            int result;
            result = 0;
            db.Employees.InsertOnSubmit(obj);
            try
            {
                db.SubmitChanges();
                result = obj.EmployeeID;
            }
            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show(e.Message);
                result = -1;
            }
            return result;
        }

        public int DelEmployeeByID(int empID)
        {
            var emp = from emps in db.Employees
                      where emps.EmployeeID == empID
                      select emps;
            db.Employees.DeleteAllOnSubmit(emp);
            db.SubmitChanges();
            return 1;
        }
        #endregion

        #region WorkWithKnowall

        public IEnumerable<Post> GetAllPost()
        {
            var query = from posts in db.Posts
                        select posts;
            return query;
        }



        #endregion



    }
}
